#include <ctime>
#include <fstream>
#include <vector>
#include <Format.h>
#include "global_const.h"
#include "comm.h"
#include "student.h"
#include "computer_lab.h"

Student::Student()
{
}

Student::Student(int id, string name, string password)
{
    m_id = id;
    m_name = name;
    m_password = password;
}
Student::~Student()
{
}

void Student::menu()
{
    cout << "\t\t ----------------------------------\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|          1.申请预约              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|          2.查看我的预约          |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|          3.查看所有预约          |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|          4.取消预约              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t|          0.注销登录              |\n";
    cout << "\t\t|                                 |\n";
    cout << "\t\t ----------------------------------\n";
    cout << "请选择您的操作： " << endl;
}

void Student::action(int choice)
{
    switch (choice)
    {
    case 1: // 申请预约
    {
        this->lab_rsvt();
        break;
    }
    case 2: // 查看我的预约
    {
        this->search_self_rsvt();
        break;
    }
    case 3: // 查看所有预约
    {
        this->search_all_rsvt();
        break;
    }
    case 4: // 取消预约
    {
        this->cancle_rsvt();
        break;
    }
    default:
        cout << "选择错误" << endl;
        break;
    }
}

void Student::lab_rsvt()
{
    cout << "机房开放时间为周一至周五！" << endl;
    cout << "请输入申请预约的时间：" << endl;
    cout << "1、周一" << endl;
    cout << "2、周二" << endl;
    cout << "3、周三" << endl;
    cout << "4、周四" << endl;
    cout << "5、周五" << endl;
    int week = 0;
    int interval = 0;
    int lab = 0;

    while (true)
    {
        cin >> week;
        if (1 <= week && week <= 5)
            break;
        cout << "输入有误，请重新输入" << endl;
    }

    cout << "请输入申请预约的时间段：" << endl;
    cout << "1、上午" << endl;
    cout << "2、下午" << endl;
    while (true)
    {
        cin >> interval;
        if (1 <= interval && interval <= 2)
            break;
        cout << "输入有误，请重新输入" << endl;
    }

    cout << "请选择机房：" << endl;
    show_lab();
    while (true)
    {
        cin >> lab;
        if (1 <= lab && lab <= 3)
            break;
        cout << "输入有误，请重新输入" << endl;
    }

    time_t now = time(0);
    tm *ltm = localtime(&now);
    string str_now = util::Format("{0}/{1}/{2} {3}:{4}:{5}", 1970 + ltm->tm_year, 1 + ltm->tm_mon, ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);

    ofstream ofs(RESERVATION_FILE, ios::app);
    if (ofs.tellp() == 0)
        ofs << "LabId,StuId,Week,Interval,RsvtDate,Status" << endl;
    ofs << util::Format("{0},{1},{2},{3},{4},{5}", lab, this->m_id, week, interval, str_now, 1) << endl;
    ofs.close();
    cout << "预约成功，审核中！" << endl;
}

void Student::search_self_rsvt()
{
    ifstream ifs(RESERVATION_FILE, ios::in);
    if (!ifs.is_open())
    {
        cout << "无预约记录" << endl;
    }

    string buff;
    string status;
    string interval;
    vector<string> vs;
    while (getline(ifs, buff))
    {
        vs = str_split(buff, ',');
        status = vs[5];
        interval = vs[3];
        if (interval != "Interval")
        {
            if (vs[1] != to_string(m_id))
                continue;
            interval = interval == "1" ? "上午" : "下午";
            if (status == "0")
                status = "预约成功";
            else if (status == "1")
                status = "待审核";
            else if (status == "9")
                status = "已取消";
        }
        cout << util::Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t", vs[0], vs[1], vs[2], interval, vs[4], status) << endl;
    }
}

void Student::search_all_rsvt()
{
    ifstream ifs(RESERVATION_FILE, ios::in);
    if (!ifs.is_open())
    {
        cout << "无预约记录" << endl;
        return;
    }
    string buff;
    string status;
    string interval;
    vector<string> vs;
    while (getline(ifs, buff))
    {
        vs = str_split(buff, ',');
        interval = vs[3];
        status = vs[5];
        if (interval != "Interval")
        {
            interval = interval == "1" ? "上午" : "下午";
            if (status == "0")
                status = "预约成功";
            else if (status == "1")
                status = "待审核";
            else if (status == "2")
                status = "审核未通过";
            else if (status == "9")
                status = "已取消";
        }
        cout << util::Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t", vs[0], vs[1], vs[2], interval, vs[4], status) << endl;
    }
}

void Student::cancle_rsvt()
{
    search_self_rsvt();
    fstream iofs(RESERVATION_FILE, ios::in | ios::out);
    if (!iofs.is_open())
        return;

    cout << "请输入要取消的记录(行号)" << endl;
    int choice;
    cin >> choice;

    string buff;
    string interval;
    vector<string> vs;
    int count = 0;
    while (getline(iofs, buff))
    {
        vs = str_split(buff, ',');
        if (vs[1] == to_string(m_id))
        {
            if (count == choice)
            {
                // 取当前行的倒数第二个字符覆盖它，最后一个是换行符。
                iofs.seekp((iofs.tellg() - std::streamoff(2)));
                // iofs << "9" << endl;  // 这样会另起一行
                iofs.write("9", 1);
                break;
            }
            count++;
        }
    }
    iofs.close();
    search_self_rsvt();
}